﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using XFControls.XFPanels.XFPanelItems;
using XFControls;
using System.Drawing;
using System.Windows.Forms;
using XFControls.XFPanels;
using FBEntities.FBObjects;

namespace XFItemControls.XFPanels.XFPanelItems
{
    class XFItemAddComment : IXFItemReceiveClick
    {

        private int height = 40;
        private FBPost _post;
        private bool _commentClicked;

        public XFItemAddComment(FBPost post)
        {
            _post = post;
            Style = new XFItemStyle()
            {
                PaddingTop = 2
            };
            Style.BackgroundColor = Color.RoyalBlue;
            Style.TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
            Style.TextColor = Color.White;
            Style.DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash;

            post.Updated += (o, e) =>
                {
                    ResetHeight();
                    if (Buffer != null)
                        Buffer.Dispose();

                    Buffer = null;

                    var par = Parent as XFPanelList;

                    if (!par.IsDisposed && par.InvokeRequired)
                        par.Invoke(new Action(() =>
                        {
                            par.Invalidate();
                        }));
                };

            ItemType = XFItemType.Clickable;
        }

        #region IXFItem Members

        public XFItemType ItemType { get; set; }
        public XFPanelBase Parent { get; set; }
        public XFItemState State { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemStyle Style { get; set; }

        public int GetHeight()
        {
            return height + Style.PaddingTop + Style.PaddingBottom;
        }

        public void ResetHeight() { }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;
            string display = "Add Comment";
            string like = _post.PeopleWhoLiked.data.Any(l => l.id.ToString() == XFItemControlUtils.Me.Me.id) ? "Unlike" : "Like";

            XFControlUtils.DrawBackground(Style, g, x, y, width, GetHeight());
            XFControlUtils.DrawBoarders(Style, g, x, y, width, GetHeight());

            if (Parent != null)
            {
                if (Parent.AlphaBarWidth > 0)
                {
                    width -= Parent.AlphaBarWidth;

                    if (Parent.ScrollbarVisible)
                        width -= 5;
                }
            }

            int xmid = width / 4;
            int xmidLike = (width / 4) * 3;

            using (SolidBrush b = new SolidBrush(Style.TextColor))
            {
                xmid -= (int)(g.MeasureString(display, Style.TextFont).Width / 2);
                g.DrawString(display, Style.TextFont, b, xmid, y + Style.PaddingTop);
            }
            using (SolidBrush b = new SolidBrush(Style.TextColor))
            {
                xmidLike -= (int)(g.MeasureString(like, Style.TextFont).Width / 2);
                g.DrawString(like, Style.TextFont, b, xmidLike, y + Style.PaddingTop);
            }
        }

        public void GetClick(int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;
            _commentClicked = x <= (width / 2);
        }

        public XFItemClickResult GetClickResult()
        {
            if (_commentClicked)
            {
                Parent.GetInputText();

                Parent.TextEntered += (o, e) =>
                {
                    Cursor.Current = Cursors.WaitCursor;
                    Application.DoEvents();

                    var comment = _post.AddComment(e.TextEntered);

                    var par = o as XFPanelList;

                    if (!par.IsDisposed && comment != null)
                        par.Invoke(new Action(() =>
                        {
                            XFItemPost post = new XFItemPost(comment.ToPost());
                            par.Add(post);
                        }));

                    Cursor.Current = Cursors.Default;
                };
            }
            else
            {
                bool remove = _post.PeopleWhoLiked.data.Any(l => l.id.ToString() == XFItemControlUtils.Me.Me.id);
                string text = remove ? "Do you want to Unlike this post?" : "Do you want to Like this post?";
                var modal = Parent.ShowModal(XFModal.Bottom, text);
                modal.OnSuccessAction += (o, e) =>
                {
                    var par = Parent as XFPanelList;
                    int loc = par[this];

                    XFWorkerPostLike worker = new XFWorkerPostLike(_post, remove);
                    XFItemLoading loading = new XFItemLoading(worker, false);

                    if (Buffer != null)
                    {
                        Buffer.Dispose();
                        Buffer = null;
                    }

                    loading.WorkFinished += (ob, ex) =>
                    {
                        var finishedWorker = ex.Worker as XFWorkerPostLike;
                        _post = finishedWorker.ParsedPost;

                        if (Buffer != null)
                        {
                            Buffer.Dispose();
                            Buffer = null;
                        }
                    };

                    par.Add(loading, loc);
                    loading.DoWork();
                };
                modal.State = XFControls.XFPanels.XFPanelModals.XFModalState.Expanding;

                return XFItemClickResult.CustomModal;
            }

            return XFItemClickResult.TextInput;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();
            if (Style != null)
                Style.Dispose();

            Style = null;
            Buffer = null;
        }

        #endregion
    }
}
